<?php

declare(strict_types=1);

namespace Plugin\Attractions\AttractionManagement\Repository;

use App\Repository\IRepository;
use Hyperf\Collection\Arr;
use Hyperf\Collection\Collection;
use Hyperf\Database\Model\Builder;
use Plugin\Attractions\AttractionManagement\Model\AttractionScore;
use Plugin\Attractions\AttractionManagement\Model\AttractionSpot;

class AttractionSpotRepository extends IRepository
{
    public function __construct(
        protected readonly AttractionSpot $model,
    ) {}

    public function handleSearch(Builder $query, array $params): Builder
    {
        return $query
            ->when(Arr::get($params, 'id'), static function (Builder $query, $id) {
                $query->whereIn('id', Arr::wrap($id));
            })
            ->when(Arr::get($params, 'name'), static function (Builder $query, $name) {
                $query->where('name', 'like', '%' . $name . '%');
            })
            ->when(Arr::get($params, 'positionId'), static function (Builder $query, $position_id) {
                $query->where('position_id', $position_id);
            })
            ->when(Arr::get($params, 'typeId'), static function (Builder $query, $type_id) {
                $query->where('type_id', $type_id);
            })
            ->when(Arr::get($params, 'attractionId'), static function (Builder $query, $attraction_id) {
                $query->where('attraction_id', $attraction_id);
            })
            ->when(Arr::get($params, 'showType'), static function (Builder $query, $show_type) {
                $query->where('show_type', $show_type);
            })
            ->when(Arr::get($params, 'status'), static function (Builder $query, $status) {
                $query->where('status', $status);
            })
            ->when(Arr::get($params, 'sort'), static function (Builder $query, $sort) {
                $query->orderBy('sort', $sort == 1  ? 'asc' : 'desc');
            });
    }

    public function list(array $params = []): Collection
    {
        return $this->perQuery($this->getQuery(), $params)
            ->get(['*']);
    }

    public function listDeleteIds(array $params = []): array
    {
        $res = $this->perQuery($this->getQuery(), $params)
            ->get(['position_id'])
            ->toArray();
        $delete = [];
        foreach ($res as $item) {
            $delete['position'][] = $item['position_id'];
        }
        return $delete;
    }

    public function showListPage(array $params): array
    {
        $page = intval(Arr::get($params, 'page', 1));
        $pageSize = intval(Arr::get($params, 'pageSize', 20));
        return $this->perQuery($this->getQuery(), $params)
            ->with([
                'position:id,name,longitude,latitude,altitude',
                'type:id,name',
                'tags:spot_id,tag,sort'
            ])
            ->simplePaginate($pageSize, ['id', 'name', 'type_id as typeId', 'intro_title as introTitle', 'position_id as positionId', 'attraction_id as attractionId', 'cover_id as coverId', 'show_type as showType', 'extension', 'status', 'sort'], 'page', $page)
            ->items();
    }

    public function getDetail(int $id)
    {
        return $this->getQuery()
            ->where('id', $id)
            ->with([
                'position:id,name,longitude,latitude,altitude',
                'type:id,name',
                'tags:spot_id,tag,sort'
            ])
            ->firstOrFail(['id', 'name', 'type_id as typeId', 'intro_title as introTitle', 'position_id as positionId', 'intro', 'attraction_id as attractionId', 'cover_id as coverId', 'show_type as showType', 'icon_id as iconId', 'extension', 'status', 'sort'])
            ->toArray();
    }

    public function handleOrderBy(Builder $query, $params): Builder
    {
        return $query;
    }

    public function getIds(array $params): array
    {
        return $this->perQuery($this->getQuery(), $params)
            ->pluck('id')
            ->toArray();
    }
}
